﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace SdmxMl.UiEditors
{
    /// <summary>
    /// Summary description for GridForm.
    /// </summary>
    public class GridForm : System.Windows.Forms.Form
    {
        private DataTable table;
        private bool readOnly;
        private System.Windows.Forms.Button buttonCancel;
        private System.Windows.Forms.Button buttonSave;
        private DataGridView dataGrid;
        private DataGridViewTextBoxColumn colLanguage;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public IWindowsFormsEditorService _wfes;
        
        /// <summary>
        /// Content to put in datagrid.
        /// </summary>
        public DataTable Content
        {
            get { return table; }
        }

        /// <summary>
        /// ReadOnly status.
        /// </summary>
        public bool ReadOnly
        {
            get { return readOnly; }
            set { readOnly = value; }
        }

        /// <summary>
        /// Title.
        /// </summary>
        public string Title
        {
            set { this.Text = value; }
        }

        /// <summary>
        /// Construct a GridForm object.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="canInsertRow">true if row insert allowed</param>
        /// <param name="canDeleteRow">true if row delete allowed</param>
        public GridForm(DataTable table, bool canInsertRow, bool canDeleteRow)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            this.table = table;
            this.dataGrid.DataSource = table;
            this.dataGrid.ReadOnly = ReadOnly;
            this.dataGrid.Visible = true;

            // Insert, delete allowed ?
            CurrencyManager bm = (CurrencyManager)dataGrid.BindingContext[dataGrid.DataSource, dataGrid.DataMember];
            ((DataView)bm.List).AllowNew = canInsertRow;
            ((DataView)bm.List).AllowDelete = canDeleteRow;

            this.dataGrid.Rows[0].Cells[1].Selected = true;
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.buttonCancel = new System.Windows.Forms.Button();
            this.buttonSave = new System.Windows.Forms.Button();
            this.dataGrid = new System.Windows.Forms.DataGridView();
            this.colLanguage = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
            this.SuspendLayout();
            // 
            // buttonCancel
            // 
            this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.buttonCancel.Location = new System.Drawing.Point(119, 145);
            this.buttonCancel.Name = "buttonCancel";
            this.buttonCancel.Size = new System.Drawing.Size(64, 23);
            this.buttonCancel.TabIndex = 2;
            this.buttonCancel.Text = "Cancel";
            this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
            // 
            // buttonSave
            // 
            this.buttonSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.buttonSave.Location = new System.Drawing.Point(39, 145);
            this.buttonSave.Name = "buttonSave";
            this.buttonSave.Size = new System.Drawing.Size(64, 23);
            this.buttonSave.TabIndex = 1;
            this.buttonSave.Text = "Ok";
            this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
            // 
            // dataGrid
            // 
            this.dataGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.colLanguage});
            this.dataGrid.Location = new System.Drawing.Point(-1, 1);
            this.dataGrid.Name = "dataGrid";
            this.dataGrid.RowHeadersVisible = false;
            this.dataGrid.Size = new System.Drawing.Size(219, 138);
            this.dataGrid.TabIndex = 3;
            // 
            // colLanguage
            // 
            this.colLanguage.DataPropertyName = "Language";
            this.colLanguage.HeaderText = "Language";
            this.colLanguage.Name = "colLanguage";
            this.colLanguage.Width = 70;
            // 
            // GridForm
            // 
            this.AcceptButton = this.buttonSave;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.buttonCancel;
            this.ClientSize = new System.Drawing.Size(219, 170);
            this.Controls.Add(this.dataGrid);
            this.Controls.Add(this.buttonCancel);
            this.Controls.Add(this.buttonSave);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
            this.Name = "GridForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Grid Editor";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.GridForm_FormClosing);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion


        private void buttonSave_Click(object sender, System.EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            dataGrid.EndEdit();
            if (_wfes != null)
                _wfes.CloseDropDown();
        }

        private void GridForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_wfes != null)
                _wfes.CloseDropDown();

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (_wfes != null)
                _wfes.CloseDropDown();

        }
    }

}
